package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务，参数={}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        if (application.getId()==null){//新增业务
            application.setCreateTime(new Date());
            application.setStatus(ApplicationStatusEnum.PENDING.getCode());
            /* 问题：新增申请单时，对应审批单数据中没有当前申请单的id
            * 原因：新增操作时还没有id值
            * 解决方法：在方法对应的SQL上加 useGeneratedKeys="true" keyProperty="id"
            * 作用：JDBC会为传入的application对象自动生成自增主键，并且把自增主键的值赋给当前application对象的id属性*/
            applicationMapper.insertApplication(application);
            //对应生成审批单
            auditService.insertAudit(application);
        }else {//更新业务

        }
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("加载申请单业务，参数：{}",applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        for (int i = 0;i<list.size();i++){
            ApplicationVO applicationVO = list.get(i);
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请业务，参数={}",id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //调用删除对应审批单的方法
        //删除指定申请单对应的审批单数据
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务，参数={},{}",applicationId,vehicleId);
        //更新车辆表
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
        //更新申请单表
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("归还车辆业务，参数={},{}",applicationId,vehicleId);
        //更新车辆表
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
        //更新申请单表
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
    }

    private void assignAuditUserList(ApplicationVO applicationVO){
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        List<AuditVO> auditVOList = auditService.selectAuditByApplicationId(applicationVO.getId());
        for (int i=0;i<auditVOList.size();i++){
            AuditVO auditVO = auditVOList.get(i);
            auditUserIdList.add(auditVO.getAuditUserId());
            UserVO userVO = userMapper.selectById(auditVO.getAuditUserId());
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username:auditUsernameList){
            stringJoiner.add(username);
        }
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
